package com.lsh.oms.core.proxy.service;

import com.lsh.oms.core.model.http.HttpMessage;
import com.lsh.oms.core.utils.HttpUtils;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author zhangbiao
 * @date 18/5/16
 * 货损单 推送Proxy
 */
@Component
public class WgOrderLossPushProxy {

    private static Logger logger = LoggerFactory.getLogger(WgOrderLossPushProxy.class);

    private Header[] getRequestHeader(Long venderId) {
        Header[] headers = {new BasicHeader("Accept", "application/json"),
                new BasicHeader("VENDERID", venderId + ""),
                new BasicHeader("Content-type", "application/json")};
        return headers;
    }

    /**
     * @param url
     * @param data
     * @return
     */
    public String push(String url, String data, Long venderId) {
        logger.info("货损单:发送地址:" + url + "   发送数据:" + data);
        String responseData = "";
        try {
            HttpMessage httpMessage = HttpUtils.sendPostRequest(url, data, getRequestHeader(venderId));
            responseData = httpMessage.getMessage();
            if (responseData == null) {
                responseData = "";
            }
            logger.info("货损单:WG返回 responseData:" + responseData);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("货损单:推送信息出现异常");
        }

        return responseData;
    }

}
